package com.neo.rabbitmq.seven;

import com.neo.rabbitmq.utils.RabbitMqUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @ClassName: ReceiveLogsTopic02
 * @Title: rabbitmq-demo
 * @Package: com.neo.rabbitmq.seven
 * @Description: 消费者C2
 * @Author: Kisen
 * @Date: 2021/9/15 22:53
 */
public class ReceiveLogsTopic02 {

    public static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = RabbitMqUtil.getChannel();
        // 声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");
        // 声明队列
        String queueName = "Q2";
        channel.queueDeclare(queueName, false, false, false, null);
        // 绑定交换机与队列
        channel.queueBind(queueName, EXCHANGE_NAME, "*.*.rabbit");
        channel.queueBind(queueName, EXCHANGE_NAME, "lazy.#");
        System.out.println("等待接收消息......");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("接收队列：" + queueName + " 绑定键：" + message.getEnvelope().getRoutingKey() + "，消息：" + new String(message.getBody(), "UTF-8"));
        };
        // 接收消息
        channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
        });
    }
}
